package com.yang.controller;

import com.yang.dao.EmailTemplateDao;
import com.yang.entity.ElMenu;
import com.yang.entity.EmailTemplate;
import com.yang.config.NoteListConfig;
import com.yang.service.ElMenuService;
import com.yang.service.EmailTemplateServer;
import com.yang.utils.info.NoteResponseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * @author yangdacheng
 * @title: NoteListController 程序配置类
 * @projectName note_api
 * @description: TODO
 * @date 2022/3/7 6:43 下午
 */

@RestController
@RequestMapping("noteList")
@Slf4j
public class NoteListController {

    @Autowired
    private NoteListConfig noteListConfig;
    @Resource
    private EmailTemplateServer emailTemplateServer;
    @Resource
    private ElMenuService elMenuService;

    /**
     * 获取应用程序的基本信息
     * @return
     */
    @GetMapping("getBaseContext")
    public NoteResponseInfo getAppBaseContext(){
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        if (noteListConfig.getAppTitle() != null){
            noteResponseInfo.setNoteCode(200).setNoteMessage("获取成功").setNoteClass(noteListConfig);
        }
        return noteResponseInfo;
    }

    /**
     * 获取邮件模板
     * @return
     */
    @GetMapping("getEmailTemplate/{tid}")
    public NoteResponseInfo getEmailTemplate(@PathVariable("tid") String tid){
        EmailTemplate templateByID = emailTemplateServer.getTemplateByID(tid);
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        if (templateByID != null){
            noteResponseInfo.setNoteCode(200).setNoteMessage("获取成功").setNoteClass(templateByID);
        }
        return noteResponseInfo;
    }

    /**
     * 修改基本配置文件
     * @return
     */
    @PostMapping("setAppBaseContext")
    public NoteResponseInfo setAppBaseContext(NoteListConfig noteListConfig){
        log.info(noteListConfig.toString());
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        Properties properties = new Properties();
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("src/main/resources/noteList.properties");
            properties.setProperty("note.appTitle",noteListConfig.getAppTitle());
            properties.setProperty("note.openSendEmail",noteListConfig.getOpenSendEmail().toString());
            properties.setProperty("note.openSendTel",noteListConfig.getOpenSendTel().toString());
            properties.setProperty("note.openUpdateUser",noteListConfig.getOpenUpdateUser().toString());
            properties.setProperty("note.openUseTel",noteListConfig.getOpenUseTel().toString());
            properties.store(fileOutputStream,"新的文件");
            noteResponseInfo.setNoteCode(200).setNoteMessage("修改成功");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return noteResponseInfo;
    }

    /**
     * 修改邮箱模板
     * @param emailTemplate
     * @return
     */
    @PutMapping("setEmailTemplate")
    public NoteResponseInfo setEmailTemplate(EmailTemplate emailTemplate){
        Boolean updTemplate = emailTemplateServer.updTemplateByID(emailTemplate);
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        if (updTemplate){
            noteResponseInfo.setNoteCode(200).setNoteMessage("修改成功").setNoteClass(emailTemplate);
        }
        return noteResponseInfo;
    }

    /**
     * 获取应用程序的菜单栏
     * @return
     */
    @GetMapping("getElMenu")
    public NoteResponseInfo getElMenu(){
        List<ElMenu> elMenus = elMenuService.queryAllEl();
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        if (elMenus != null){
            noteResponseInfo.setNoteCode(200).setNoteMessage("获取成功").setNoteClass(elMenus);
        }
        return noteResponseInfo;
    }

    /**
     * 修改菜单信息
     * @param elMenu
     * @return
     */
    @PutMapping("updElMenu")
    public NoteResponseInfo updElMenu(ElMenu elMenu){
        Boolean updElMenu = elMenuService.updElMenu(elMenu);
        NoteResponseInfo noteResponseInfo = new NoteResponseInfo();
        if (updElMenu){
            noteResponseInfo.setNoteCode(200).setNoteMessage("修改成功").setNoteClass("");
        }
        return noteResponseInfo;
    }
}
